package com.shensi.adminCollect.controller.market;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.entity.SysDept;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.markert.MarkertPlan;
import com.shensi.adminCollect.service.markert.IMarkertPlanService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 营销方案Controller
 *
 * @author qujia
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/market/plan")
public class MarkertPlanController extends BaseController
{
    @Autowired
    private IMarkertPlanService markertPlanService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询营销方案列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            markertPlan.setStationIdList(list);
            markertPlan.setStationId(null);
        }
        markertPlan.setStatus("0");
        startPage();
        List<MarkertPlan> planList =new ArrayList<>();
        if(markertPlan.getDiscountPlanType()!=null && markertPlan.getDiscountPlanType().equals("4")){
            planList  = markertPlanService.searchMarkertPlanList(markertPlan);
        }else{
            planList  = markertPlanService.selectMarkertPlanList(markertPlan);
        }
        return getDataTable(planList);
    }

    /**
     * 导出营销方案列表
     */
    @GetMapping("/export")
    public AjaxResult export(MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            markertPlan.setStationIdList(list);
            markertPlan.setStationId(null);
        }
        markertPlan.setStatus("0");
        List<MarkertPlan> planList = markertPlanService.selectMarkertPlanList(markertPlan);
        ExcelUtil<MarkertPlan> util = new ExcelUtil<MarkertPlan>(MarkertPlan.class);
        return util.exportExcel(planList, "plan");
    }

    /**
     * 获取营销方案详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(markertPlanService.selectMarkertPlanById(id));
    }

    /**
     * 新增营销方案
     */
    @PostMapping
    public AjaxResult add(@RequestBody MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setStationId(currentUser.getUser().getDeptId());
        markertPlan.setCreateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setCreateTime(DateUtils.getNowDate());
        markertPlan.setStatus("0");
        return toAjax(markertPlanService.insertMarkertPlan(markertPlan));
    }

    /**
     * 修改营销方案
     */
    @PutMapping
    public AjaxResult edit(@RequestBody MarkertPlan markertPlan)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setStationId(currentUser.getUser().getDeptId());
        markertPlan.setUpdateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setUpdateTime(DateUtils.getNowDate());
        markertPlan.setStatus("0");
        return toAjax(markertPlanService.updateMarkertPlan(markertPlan));
    }
    /**
     * 新增阶梯营销方案
     */
    @PostMapping("/updateMartketPlan")
    @Transactional
    public AjaxResult updateMartketPlan(@RequestBody MarkertPlan markertPlan)
    {
        int i =0;
        if(markertPlan.getId() !=null) {
            MarkertPlan markertPlan1 = markertPlanService.selectMarkertPlanById(markertPlan.getId());
            markertPlan1.setStatus("2");
            i = markertPlanService.updateMarkertPlan(markertPlan1);
            if (i != 0) {
                markertPlan.setId(null);
                i = markertPlanService.insertMarkertPlan(markertPlan);
            }
        }else{
            i = markertPlanService.insertMarkertPlan(markertPlan);
        }
        return toAjax(i);
    }

    /**
     * 删除营销方案
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        int i =0;
        MarkertPlan markertPlan =new MarkertPlan();
        LoginUser currentUser = SecurityUtils.getLoginUser();
        markertPlan.setUpdateBy(currentUser.getUser().getUserId()+"");
        markertPlan.setUpdateTime(DateUtils.getNowDate());
        markertPlan.setStatus("1");
        markertPlan.setId(ids);
        i=markertPlanService.updateMarkertPlan(markertPlan);
        return toAjax(i);
    }

    /**
     * 新增阶梯营销方案
     */
    @PostMapping("/addMarketPlanInfo")
    @Transactional
    public AjaxResult addMarketPlanInfo(@RequestBody MarkertPlan markertPlan)
    {
        int i =0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //查询所有的数据
        MarkertPlan plans =new MarkertPlan();
        plans.setDiscountPlanType("4");
        plans.setStationId(currentUser.getUser().getDeptId());
        plans.setOilName(markertPlan.getOilName());
        List<MarkertPlan> markertPlans  =markertPlanService.selectMarkertPlanList(plans);
        if(markertPlans!=null && markertPlans.size()>0){
            for(MarkertPlan plan:markertPlans){
                i= markertPlanService.deleteMarkertPlanById(plan.getId());
            }
        }
        if(markertPlan!=null && markertPlan.getList()!=null && markertPlan.getList().size()>0){
            for(MarkertPlan plan:markertPlan.getList()){
                plan.setStationId(currentUser.getUser().getDeptId());
                plan.setCreateBy(currentUser.getUser().getUserId()+"");
                plan.setCreateTime(DateUtils.getNowDate());
                plan.setStatus("0");
                plan.setGrade(markertPlan.getGrade());
                plan.setDiscountTerm(markertPlan.getDiscountTerm());
                plan.setVipDiscountyPlus(markertPlan.getVipDiscountyPlus());
                plan.setCouponPlus(markertPlan.getCouponPlus());
                plan.setOilName(markertPlan.getOilName());
                plan.setDiscountPlanType(markertPlan.getDiscountPlanType());
                i= markertPlanService.insertMarkertPlan(plan);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除营销方案
     */
    @PostMapping("/deleteMarketPlan")
    public AjaxResult deleteMarketPlan(@RequestBody MarkertPlan markertPlan)
    {
       int i=0;
        LoginUser currentUser = SecurityUtils.getLoginUser();
       if(markertPlan!=null && markertPlan.getOilName()!=null) {
         markertPlan.setStationId(currentUser.getUser().getDeptId());
         i = markertPlanService.deleteMarkertPlan(markertPlan);
       }
       return toAjax(i);
    }
}
